public class _4minOperations {
    //将x减到0的最小操作数
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        int right = 0,left = 0;
        for(int a : nums) sum+=a;
        int target = sum - x;
        int len = -1;
        //细节处理
        if(target < 0){
            return -1;
        }
        sum = 0;
        while(right <nums.length){

            sum+=nums[right];
            while(sum > target){
                sum -= nums[left++];
            }
            if(sum == target){
                len = Math.max(len,right-left+1);
            }
            right++;
        }
        if(len == -1) return -1;
        else return nums.length-len;
    }
}
